<?php

namespace App\Rules;

use App\Models\TradeQueue;
use Illuminate\Contracts\Validation\Rule;

class CaptchaCode implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $captchaData = \Cache::get(request('captcha_key'));
        if (!hash_equals($captchaData['payload']['code'], $value)) {
            // 验证错误就清除缓存
            \Cache::forget(request('captcha_key'));
            return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return ['error_code'=>'F005005','message'=>'图片验证码错误！'];

        return 'The validation error message.';
    }
}
